//
//  SaleSetPriceVC.m
//  New_Runway
//
//  Created by mack on 2018/1/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "SaleSetPriceVC.h"
#import "CarSetListCell.h"
#import "CarBaseInfoModel.h"
#import "CrmIsArriveShopCell.h"

@interface SaleSetPriceVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, assign) BOOL isTransFee;//是否包含过户费
@property (nonatomic, assign) BOOL isOnePrice;//是否一口价
@property (nonatomic, strong) CarBaseInfoModel *infomodel;
@property (nonatomic, assign) BOOL isPrice;
@property (nonatomic, copy) NSString *shopID;


@end

@implementation SaleSetPriceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"销售定价";
    self.view.backgroundColor = kColorWhite();
    [self.navigationBarView.rightItem setTitle:@"确定" forState:UIControlStateNormal];
    self.infomodel = self.parems[@"info"];

    self.isTransFee = self.infomodel.result.carTransferState;
    self.isOnePrice = self.infomodel.result.carFixedPriceState;
    self.shopID = self.infomodel.result.shopId;
    [self initTableview];
    
    
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableview registerClass:[CarSetListCell class] forCellReuseIdentifier:@"CarSetListCell"];
    [self.tableview registerNib:[UINib nibWithNibName:@"CrmIsArriveShopCell" bundle:nil] forCellReuseIdentifier:@"CrmIsArriveShopCell"];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_equalTo(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 45;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (self.isPrice == NO) {
            return 2;
        }else{
            return 1;
        }
    }
    
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            CrmIsArriveShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CrmIsArriveShopCell"];
            cell.index = indexPath;
            if (self.isPrice == YES) {
                [cell.isYesB setImage:[UIImage imageNamed:@"crm_seleted_s"] forState:UIControlStateNormal];
                [cell.isYesB setTitleColor:MAIN_NAV_COLOR() forState:UIControlStateNormal];
                [cell.isNoB setImage:[UIImage imageNamed:@"crm_seleted_n"] forState:UIControlStateNormal];
                [cell.isNoB setTitleColor:MAIN_FONT_COLOR() forState:UIControlStateNormal];
            }else{
                [cell.isYesB setImage:[UIImage imageNamed:@"crm_seleted_n"] forState:UIControlStateNormal];
                [cell.isYesB setTitleColor:MAIN_FONT_COLOR() forState:UIControlStateNormal];
                [cell.isNoB setImage:[UIImage imageNamed:@"crm_seleted_s"] forState:UIControlStateNormal];
                [cell.isNoB setTitleColor:MAIN_NAV_COLOR() forState:UIControlStateNormal];
            }
            cell.mianLabel.text = @"价格面议";
            return cell;
            
        }
        
    }
   
    
    
    CarSetListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarSetListCell"];
    cell.centerTF.tag = indexPath.row;
    cell.centerTF.placeholder = @"请输入金额";
    cell.centerTF.delegate = self;
    cell.centerTF.keyboardType = UIKeyboardTypeDecimalPad;
    cell.switchButton.hidden = YES;
    cell.centerLabel.hidden = YES;
    [cell.rightImage setTitle:@"万元" forState:UIControlStateNormal];
    [cell.rightImage setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    cell.centerTF.hidden = NO;
    cell.rightImage.hidden = NO;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 0) {
        
        if (self.isPrice == NO) {
            
            if (indexPath.row == 1){
                cell.label.text  = @"零售价";
                cell.centerTF.text = [NSString stringWithFormat:@"%.2f",self.infomodel.result.carRetailPrice];
            }
        }
        
    }else{
        NSArray *array = @[@"包含过户费",@"是否一口价"];
        cell.label.text  = array[indexPath.row];
        if (indexPath.row == 0 ){
            cell.centerTF.hidden = YES;
            cell.rightImage.hidden = YES;
            cell.switchButton.hidden = NO;
            [cell.switchButton setOn:self.isTransFee];
            
        }else if (indexPath.row == 1){
            cell.centerTF.hidden = YES;
            cell.rightImage.hidden = YES;
            cell.switchButton.hidden = NO;
            [cell.switchButton setOn:self.isOnePrice];
            
        }
    }
    
 
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            if (_isTransFee == NO) {
                _isTransFee = YES;
            }else{
                _isTransFee = NO;
            }
            [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:NO];
            
        }else if (indexPath.row == 1){
            
            if (_isOnePrice == NO) {
                _isOnePrice = YES;
            }else{
                _isOnePrice = NO;
            }
            [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:1]] withRowAnimation:NO];
            
        }
    }
   
    
    
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if ([text returnRealWithFloatLength:2] == YES) {
        self.infomodel.result.carRetailPrice = [text floatValue];
    }
    return [text returnRealWithFloatLength:2];
}
-(void)rightItemClick{
    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
    [self updataCarPrice];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)updataCarPrice{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,updataCarApi];
    NSString *price = [NSString stringWithFormat:@"%.2f",self.infomodel.result.carRetailPrice];
    NSDictionary *params = @{
                             @"shopId":self.shopID,
                             @"carId":self.infomodel.result.carId,
                             @"carFixedPriceState":@(_isOnePrice),
                             @"carTransferState":@(_isTransFee),
                             @"carRetailPrice":price,
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            [ShowMessage showHintMessage:@"销售定价成功" duration:2.0 withRemindId:remindSuccess];
            NSNotification *notificatiom = [NSNotification notificationWithName:@"setPrice" object:self.indexPath userInfo:@{@"type":@"price",@"value":price}];
            [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [ShowMessage showHintMessage:response[@"msg"] duration:2.0 withRemindId:remindFail];
        }
        
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
}
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    NSIndexPath *index = userInfo[@"index"];
    if ([eventName isEqualToString:@"ISYES"]) {
        if (index.section == 0 && index.row == 0) {
            self.isPrice = YES;
            self.infomodel.result.carRetailPrice = 0;
        }
        
    }else if ([eventName isEqualToString:@"ISNO"]){
        if (index.row == 0 && index.section == 0) {
            self.isPrice = NO;
            self.infomodel.result.carRetailPrice = self.infomodel.result.carRetailPrice;
            
        }
    }
    [self.tableview reloadData];
    
}

@end
